package com.ziyueyuwen.xiniao.schedule;

import com.alibaba.fastjson.JSONObject;
import com.suyeer.cache.RedisUtil;
import com.ziyueyuwen.xiniao.bean.ZyClassInfo;
import com.ziyueyuwen.xiniao.bean.ZyClassLessonInfo;
import com.ziyueyuwen.xiniao.bean.ZyLessonMessageTask;
import com.ziyueyuwen.xiniao.dao.ZyClassLessonInfoMapper;
import com.ziyueyuwen.xiniao.enums.ClassStatusEnum;
import com.ziyueyuwen.xiniao.service.ClassService;
import com.ziyueyuwen.xiniao.service.LessonService;
import com.ziyueyuwen.xiniao.service.MessageTaskTemplateService;
import org.apache.commons.lang3.time.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import static com.ziyueyuwen.xiniao.util.ConstUtil.SYS_CONFIG_DATA;

/**
 * @author wancheng  on 2018/12/21.
 */
@Component
@EnableScheduling
public class MessageTaskSchedule {


    private final static Logger logger = LoggerFactory.getLogger(MessageTaskSchedule.class);

    private final static Integer MINUTE = 1;

    private final static String JOB_NAME_SEND_TASK = "sendTask";

    @Autowired
    MessageTaskTemplateService messageTaskTemplateService;
    @Autowired
    ClassService classService;
    @Autowired
    ZyClassLessonInfoMapper lessonInfoMapper;

    @Scheduled(cron = "0 0/1 * * * ?")
    public void sendTask() {
        Date currentDate = DateUtils.truncate(new Date(), Calendar.MINUTE);
        Date after = DateUtils.addMinutes(currentDate, MINUTE);
        if (SYS_CONFIG_DATA.getIfRunTaskJob() && RedisUtil.isEnableRunTask(JOB_NAME_SEND_TASK)) {
            List<ZyLessonMessageTask> tasks = messageTaskTemplateService.getTask(currentDate, after);
            messageTaskTemplateService.executeTask(tasks);
            logger.info("send task execute, data:{}", JSONObject.toJSON(tasks));
        }
    }


    @Scheduled(cron = "0 0/1 * * * ?")
    public void updateClassStatus() {
        updateUnStart();
        //updateImplementing();
    }

    private void updateUnStart() {
        Date currentDate = new Date();
        List<ZyClassInfo> classInfos = classService.getClassByStatus(ClassStatusEnum.CLASS_UN_START);
        if (!CollectionUtils.isEmpty(classInfos)) {
            classInfos.forEach(
                    classInfo -> {
                        try {
                            ZyClassLessonInfo param = new ZyClassLessonInfo();
                            param.setClassId(classInfo.getId());
                            param.setLessonIndex(1);
                            ZyClassLessonInfo lessonInfo = lessonInfoMapper.selectOne(param);
                            if (lessonInfo == null){
                                return;
                            }
                            if (lessonInfo.getBeginTime().before(currentDate)) {
                                classService.updateClassStatus(classInfo.getId(), ClassStatusEnum.CLASS_IMPLEMENTING);
                                logger.info("修改班级状态 classInfo={}", JSONObject.toJSON(classInfo));
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
            );
        }
    }

    private void updateImplementing() {
        Date currentDate = new Date();
        List<ZyClassInfo> classInfos = classService.getClassByStatus(ClassStatusEnum.CLASS_IMPLEMENTING);
        if (!CollectionUtils.isEmpty(classInfos)) {
            classInfos.forEach(
                    classInfo -> {
                        try {
                            if (classInfo.getEndTime().after(currentDate)) {
                                classService.updateClassStatus(classInfo.getId(), ClassStatusEnum.CLASS_ENDING);
                            }
                        } catch (Exception e) {
                            logger.info("classinfo = {}", JSONObject.toJSON(classInfo));
                            e.printStackTrace();
                        }
                    }
            );
        }
    }
}
